﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TagCore;

namespace TagLib
{
    public class TagData : IMusicData
    {
        #region Data
        private TagLib.Tag tag;
        #endregion

        #region C'tor
        public TagData(TagLib.Tag tag)
        {
            this.tag = tag;
        }
        #endregion

        #region Public Properties
        public string Title
        {
            get
            {
                return tag.Title;
            }
            set
            {
                tag.Title = value;
            }
        }

        public string[] Artists
        {
            get
            {
                return tag.Performers;
            }
            set
            {
                tag.Performers = value;
            }
        }


        public byte[] Image
        {
            get
            {
                return GetCoverArt();
            }
            set
            {
                SetCoverArt(value);
            }
        }
        #endregion

        private byte[] GetCoverArt()
        {
            IPicture pic = FindCoverArt();
            if (pic != null)
            {
                byte[] raw = new byte[pic.Data.Count];
                pic.Data.CopyTo(raw, 0);
                return raw;
            }
            else
            {
                return null;
            }
        }

        private IPicture FindCoverArt()
        {
            if (tag.Pictures.Length > 0)
            {
                foreach (IPicture pic in tag.Pictures)
                {
                    if (pic.Type == PictureType.FrontCover)
                        return pic;
                }
                // else return first
                return tag.Pictures[0];
            }
            return null;
        }

        private void SetCoverArt(byte[] raw)
        {
            List<IPicture> pics = new List<IPicture>();
            IPicture sourcePic = tag.Pictures.Length > 0 ? tag.Pictures[0] : null;

            for(int loop = 0; loop < tag.Pictures.Length; loop++)
            {
                IPicture pic = tag.Pictures[loop];
                if (pic.Type == PictureType.FrontCover)
                    sourcePic = pic;
                else if (loop > 0)
                    pics.Add(pic);
            }

            if (sourcePic == null)
                sourcePic = new Id3v2.AttachedPictureFrame(new TagLib.Picture(new ByteVector(raw, raw.Length)));
            else
                sourcePic.Data = new ByteVector(raw, raw.Length);

            sourcePic.MimeType = "image/jpeg";
            sourcePic.Type = PictureType.FrontCover;
            pics.Insert(0, sourcePic);
            tag.Pictures = pics.ToArray();
        }

        public string[] AlbumArtists
        {
            get
            {
                return tag.AlbumArtists;
            }
            set
            {
                tag.AlbumArtists = value;
            }
        }

        public string Genre
        {
            get
            {
                if (tag.Genres.Length > 0)
                    return tag.Genres[0];
                else
                    return string.Empty;
            }
            set
            {
                tag.Genres = new string[] {value};
            }
        }

        public string Comments
        {
            get
            {
                return tag.Comment;
            }
            set
            {
                tag.Comment = value;
            }
        }

        public uint Track
        {
            get
            {
                return tag.Track;
            }
            set
            {
                tag.Track = value;
            }
        }


        public string Album
        {
            get
            {
                return tag.Album;
            }
            set
            {
                tag.Album = value;
            }
        }


        public uint Year
        {
            get
            {
                return tag.Year;
            }
            set
            {
                tag.Year = value;
            }
        }
    }
}
